package com.zhan.ielts.web.controller;

import com.zhan.frame.core.web.domain.Result;
import com.zhan.ielts.domain.ExamTime;
import com.zhan.ielts.domain.query.ExamTimeQuery;
import com.zhan.ielts.service.ExamTimeService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;

@Controller
@RequestMapping("/examTime")
public class ExamTimeController {

	private Logger log = LoggerFactory.getLogger(ExamTimeController.class);

	public static JSONObject examTimeJson = null;

	@Autowired
	private ExamTimeService examTimeService;

	@ResponseBody
	@RequestMapping(value = "/select",method = RequestMethod.POST)
	public Result selectList(ExamTimeQuery query) {
		Result result = new Result();
		boolean flag = false;
		if(null == query.getYear()){
			if(null != examTimeJson){
				ExamTime et = examTimeService.queryById("0");
				if(null == et || 0==et.getDay()){
					result.addOK(examTimeJson);
					return result;
				}
			}
			Calendar cal = Calendar.getInstance();
			query.setYear(cal.get(Calendar.YEAR));
			flag = true;
		}
		JSONObject json = examTimeService.selectET(query);
		if(flag){
			examTimeJson = json;
		}
		result.addOK(examTimeJson);
		return result;
	}
}
